{-------------------------------------------------------------
#
#	Apple Macintosh Developer Technical Support
#
#	MacApp Color QuickDraw Fractal Printing Sample Application
#
#	FracApp300
#
#	UFracApp300.inc1.p	-	Pascal Source
#
#	Copyright © 1988 Apple Computer, Inc.
#	All rights reserved.
#
#	Versions:	1.0					8/88
#
#	Components:	MFracApp300.p		August 1, 1988
#				UFracApp300.p		August 1, 1988
#				UFracApp300.inc1.p	August 1, 1988
#				FracApp300.r		August 1, 1988
#				FracApp300.make		August 1, 1988
#
#	The FracApp300 program is a version of the FracApp program that is
#	set up to be as compatible as possible.  It uses the PrGeneral call
#	in order to print at high resolution.  It demonstrates how to create
#	and save 300 dpi PICT files.  It uses an offscreen port to calculate
#	the data, and CopyBits to update the window on the screen.  When the
#	documents are written or read, the QuickDraw bottlenecks are used to
#	avoid having a huge memory hit during saving or opening. (800K of RAM
#	not needed, on a 640x480 screen, a big win.)  The Palette Manager is
#	used very slightly, only to associate a small palette with only Black
#	and White with each window.  This avoids needing the system palette
#	for each window, when no colors are used.  Since we are printing to
#	normal printers, we only use B&W in this version.
#			Written in MacApp Object Pascal code.
#			Compatibility rating = 1.  (The use of PrGeneral is slightly 
#				out of the ordinary, although supported.)
#
#	The program is a complete Macintosh application written in Object 
#	Pascal using MacApp.  It supports multiple windows, calculations in the
#	background under MultiFinder, use of the Palette Manager, reading and
#	writing of PICT files using the bottlenecks, and shows how to calculate
#	the Mandelbrot set.
#
#	There is a resource file that is necessary as well, to define the Menus, Window,
#	Dialog, and Palette resources used in the program.  
-------------------------------------------------------------}
{FracApp300:
	Copyright 1988 by Bob.  All rights reserved, since Bob has all rights.
	February 1, 1988.
	Updated May 1, 1988:  removed the color handling stuff, turned it into
		a printing example with 300 dpi printing of B&W mandelbrots.

	Written by Bo3b Johnson of Developer Technical Support. }

{ Version 1.1:
	8/1/88: Fixed the print handler to handle printing from the Finder (oops!).  The
		handler was rearranged to more closely follow the style of the StdPrintHandler. }


{ The following is a list of features or bug fixes that could be added to the program:
  *** Add a menu option to scale to 72 DPI or not.
  *** Check the segmentation.
  *** Bug with selection rectangle on edge of document, moving by 1 pixel.
  *** Make it run crashless using temp documents to store partial fractals.
  *** Updates could be cleaner so no partial fractals are displayed.
  *** Override window.updateevent so we can avoid the EraseRect on updates.
  *** Draw selection rect in offscreen, copy up to screen for flicker free selection.
  *** Crash on 3 monitor system during window drag.
  *** Could set the bytes directly in offscreen PixMap, skip using MoveTo:Line.
  *** Copy up from small picture up to big screen gets garbage, src rect too big?
  *** Allow a way to Zoom in using coordinates.
  *** Bigger penSize for fast, lo-res fractals.  Allow user to set size of pen.
  *** Add MacApp debugging stuff like Inspect and TList object checking.
  *** Some things for the reader to do to modify the program.
  }


{ Where does it fit:
	This is a series of sample programs for those doing development
	using Color QuickDraw.  Since the whole color problem depends
	upon the exact effect desired, there are a number of answers
	to how to use colors, from the simple to the radically complex.
	These programs try to cover the gamut, so you should use 
	which ever seems appropriate.  In most cases, use the simplest
	one that will give the desired results.  The compatibility
	rating is from 0..9 where low is better.  The more known risks 
	there are the higher the rating.
	
	
	The programs (in order of compatibility):
	
		SillyBalls:
			This is the simplest use of Color QuickDraw, and does
			not use the Palette Manager.  It draws randomly colored
			balls in a color window.  This is intended to give you
			the absolute minimum required to get color on the screen.
			Written in straight Pascal code.
			Compatibility rating = 0, no known risks.
		
		FracAppPalette:
			This is a version of FracApp that uses only the Palette
			Manager.  It does not support color table animation
			since that part of the Palette Manager is not sufficient.
			The program demonstrates a full color palette that is
			used to display the Mandelbrot set.  It uses an offscreen
			gDevice w/ Port to handle the data, using CopyBits to
			draw to the window.  The Palette is automatically 
			associated with each window.  The PICT files are read
			and written using the bottlenecks, to save on memory
			useage.
			Written in MacApp Object Pascal code.
			Compatibility rating = 0, no known risks.
		
		TubeTest:
			This is a small demo program that demonstrates using the
			Palette Manager for color table animation.  It uses a 
			color palette with animating entries, and draws using the
			Palette Manager.  There are two circles of animating colors
			which gives a flowing tube effect.  This is a valid case
			for using the animating colors aspect of the Palette Manager,
			since the image is being drawn directly.
			Written in straight Pascal code.
			Compatibility rating = 0, no known risks.
		
		FracApp:
			This is the Ôcommercial qualityÕ version of FracApp.  This
			version supports color table animation, using an offscreen
			gDevice w/ Port, and handles multiple documents.  The
			CopyBits updates to the screen are as fast as possible.  The
			program does not use the Palette Manager, except to
			provide for the system palette, or color modes with less than
			255 colors.  For color table animation using an offscreen
			gDevice w/ Port, it uses the Color Manager and handles the
			colors itself.  Strict compatibility was relaxed to allow for
			a higher performance program.  This is the most ÔrealÕ of the
			sample programs.
			Written in MacApp Object Pascal code.
			Compatibility rating = 2.  (nothing will break, but it may not
				always look correct.)
		
		FracApp300:	(***)
			This doesn't support colors, but demonstrates how to create and
			use a 300 dpi bitmap w/ Port.  The bitmap is printed at full
			resolution on LaserWriters, and clipped on other printers (but
			they still print).  It demonstrates how to use a high resolution
			image as a PICT file, and how to print them out.
			Written in MacApp Object Pascal code.
			Compatibility rating = 1.  (The use of PrGeneral is slightly 
				out of the ordinary, although supported.)
}

{ Reasons for this version of reality (the strategy):
	This program is intended to show a proper way of handling large offscreen buffers
	and to demonstrate how to print high resolution images in the most compatible fashion.
	It will calculate the Mandelbrot set at 300 dots per inch in an offscreen buffer,
	scaling down the image to the screens 72 dpi so that you get the WYSIWYG of a
	typical 8.5x11 page.  The documents can be saved and opened, and are stored as
	PICT files.  They can be printed on any printer, although they will be clipped on
	anything that has less than 300 dpi resolution (like the AppleTalk ImageWriter).

	The main idea behind this program is to allow you to create and fool around
	with the Mandelbrot set, using this number cruncher wizzo computer we got
	here.  This program is intended to be a real-life program,
	done as well as possible given current constraints.  The program is supposed
	to be something that somebody (who was silly perhaps) might try to sell.
	The idea is to pretend that we are developers as well, testing the development
	world as well as making a fun program.  Well, life's a bitch as a developer
	trying to write a program of this form.  No intentional shortcuts were taken
	in the program, but some things were left out due to time constraints.  Like
	all real programs, some people will like it and some people will hate it.  I
	hope you like it, but if you don't, send me some mail telling me why.

	The overall structure of the program is to have the MacApp Document object
	handle all the data.  This includes an offscreen Port that contains
	the actual fractal data.  The View object uses the Documents data to draw
	into the window visible to the user.  The Document does all the work of
	calculating new fractals during Idle times.  It also handles saving the
	data to disk and reading it back.  The data files are PICT so as to be as
	compatible as we can.  The program handles zooming in to
	see closer views of the environment, using selection rectangles as you
	would expect.  This whole block of comments at the beginning are intended
	to describe some more macroscopic problems and structure.  In the code
	itself you will find the tactical comments dealing with how a specific
	operation is done.

	The fractal calculation is done by the CalcCity routine of the Document.  The
	Application object gets the DoIdle call, and he calls each document to
	have a pixel calculated in each document.  The calculation is done a pixel
	at a time so that it can be done in the background with no visible effect
	on the foreground app.  As each horizontal line of data is finished it is
	updated to the screen.  The algorithm is very simple.

	Allocating and using an offscreen port are found in the Document
	object, as the BuildOffWorld method.  The port offscreen is used as a drawing
	environment once a pixel is calculated, as seen in DoIdle for the Document.
	This allocation is more conventionally done in the DoMakeDocument method.
	This would work here as well, since we have a fixed 8.5x11 page size that
	we are using; but we wanted to keep this similar to the color versions of the
	program where the page size is not fixed.


	This program uses the Palette Manager in a simplistic fashion since it does
	not use color.  In order to print in the current world, it is best to use only
	black and white.  This is the Palette associated with each window when it
	is opened.  The resource ID is the same as that for the window so the Palette
	Manager will use that Palette.

	This version of the program currently uses a full page as the determining size of
	the view and thus the fractal that is calculated.  This is OK, but a better approach
	would be to allow the user to specify the limits of the Fractal they want.  This
	involves adding another dialog and save defaults type of feature but is a better
	way to solve the problem since it is not obvious what the best size would be.
	A full page is easy to use, but whenever there is no obvious best answer
	it is always better to let the user decide instead, that way they can't bitch.

	Another goal of course was to get this thing done.  A goal that tends to slide
	away as more things are added to the program, so in true Macintosh style,
	the 1.0 version of the program is somewhat limited, and may not be fully
	debugged.  Some things specifically left out: printing the documents to
	a LaserWriter with grey scales instead, using temporary documents to
	make the program crashless (so you can start up where you left off, saving
	the computation it took to get there), an option to make the pen size bigger
	so you can do a low-res fractal to begin with.  These things are all admirable
	features to add, but you have to finish version 1.0 sometime, so this was it.
	These other things will be added if possible.

	Carefully watch the 881 flag with this MPW business.  There are a number of
	ridiculous problems associated with its use.  In particular the $LOAD files
	are dangerous to use with 881 and the combination of the two will often
	end up compiling successfully into a program that is garbage and will
	crash upon running it.  This, remind yourself, is a feature of the most
	powerful development system around.  In order to build currently, the main
	program MFracApp.p should be compiled with new $LOAD files and the 881
	flag turned off.  All of the MacApp sources should be compiled with a new
	$LOAD file and 881 turned off.  The last step should be to compile the UFracApp.p
	with the 881 flag turned on, and with new $LOAD files as well.  Beware or
	be prepared to spend a lot of time on something silly.  To solve this
	problem the Make file used with FracApp has a specific compile rule for
	the UFracApp file.  UFracApp also uses its own $LOAD file, that is different
	from the MacApp and MFracApp ones.  The 881 flag is not specifically
	required, but it makes code that uses the 881 directly instead of going
	through SANE for a speed up of about 10 times.  Since we are speed freaks
	here as well, the 881 option had to be used.  Given the problems, I would
	probably skip the 881 option and do the time critical pieces in assembler.

	If you want to know how long a fractal took to calculate there is a time
	stamp saved in the file header.  It is no longer drawn in the window since
	it is not accurate when the program has run in the background or if there
	are multiple documents open.  This could be added again if desired.

	Notably I am aware of the fact that this program does not really calculate
	Fractals.  Actually it calculates and displays the Mandelbrot set which is
	not self-similar so it cannot really be called a fractal.  It is distressing to
	add to the confusion as to what fractals are, but it is too late.  For more
	information on Fractals and the Mandelbrot set (no umlaut on the o), you
	could see Mandelbrots book The Fractal Geometry of Nature, but it is
	pretty mathematical and not all that helpful.  A better source is the
	Peitgen-Richter book The Beauty of Fractals, which has a do it yourself
	section in the back.

	The program is structured primarily around the document.  The document is
	the object to create and maintain the offscreen port.  The document
	converts the offscreen data into a PICT file when saved or restored.  The
	document also does the calculation of the fractal, keeping the offscreen data
	up to date as it goes along.  The view only handles taking the document data
	and displaying it.

	For the zoom operation, there was no really great way to handle the new
	document case based on another document.  This is a little strange to be
	doing, and the structure of MacApp was such that we couldn't get to the
	data desired at the right time.  The logical place to put it in at
	DoMakeDocument was too early to have the port allocated and ready
	to start more stuff.  The problem was resolved by using global variables
	to transmit the information to the other piece of the program that
	might need it.  Essentially DoInitialState decides if this is a brand new
	base level document or a zoom in based on the state of the global variables.

	The MacApp memory management approach is used as well.  The big pieces
	used by the Documents come out of permanent memory, helping to avoid
	a crash from no memory.  When we allocate something that will be thrown
	away immediately, like a spare color table or something, it of course
	comes out of normal memory.  We did not do a full blown memory analysis
	of the program since it is such a memory hog anyway.  The mem! resource
	is set up in a form that is roughly close (a bit high) without trying to be
	extra accurate.  When a document takes 400K of RAM to open it hardly
	seems relevant to make sure the mem! is accurate to 2K.  Because of this
	somewhat cavalier approach you may not be able to open a document in
	a few cases where you really should be able to.  The mem! in use of 40K
	is close with a +2K/-10K error on how big it should really be.

	The time stamp as the elapsedTime field in the FracHeader is not fully
	accurate, so it is no longer displayed.  Currently it is set as an elapsed
	time from when it was started to when it ended, which has a number
	of problems.  This will be made more accurate in the future, probably
	using a calculation that gives us a once through the loop calculation
	in units of TimeDBRA, so we can be more machine independent.

	The QuickDraw BottleNecks are used to both read and write the actual
	fractal data from/to the disk.  This is done since the data in the document
	may be very large (500K) and if we just spool the data from the file
	we don't actually have to use that extra hunk of memory.  We have to read
	the data anyway, so we go ahead and just read it in as we play it back.
	As it is played back it goes into the offscreen port, so we
	have the data to display.  No memory hit for the document is a big win.
	When writing the data, the same thing is true so we don't have to have
	a huge handle to hold the picture data itself.  We also avoid the problem
	of not having enough memory to create the picture in the first place,
	making a document unsaveable.  That is particualarly annoying, and
	is easy to avoid using the spooling approach.

	With thanks to Skippy Blair for the discussions of color QuickDraw and the
	Palette Manager.  Thanks to Darin Adler for further discussion of the Palette
	Manager and for good suggestions on making it more MacApp friendly.  Thanks
	to Chris Derossi for providing the first 300 dpi fractal printing, and to ZZ
	Zimmerman for the next generation code to print compatibly with PrGeneral.
	}


	{ Global variables.  }
VAR
	gStaggerCount: 	INTEGER;				{ for staggering windows. }
	gRealMin,
	gRealMax,
	gImagMin,
	gImagMax:			Extended;				{ used for zooming in operation. }

		{ The next globals are used for the QuickDraw bottlenecks when reading or
			writing a picture to disk.  These are needed, since the bottlenecks cannot
			be owned procedures. }
	gPictSize:			LongInt;				{ number of bytes used for saving a PICT. }
	gPictError:			OSErr;					{ do some error handling in bottleneck. }
	gPictRefNum:		Integer;				{ Need the refnum of the open file too. }
	gPictHandle:		PicHandle;			{ for reading/writing a picture. }


{ Set some compiler options that we desire for the main body of code only.  You
	might wish to leave on the range checking, but I was not satisfied with having
	to push and pop in the code, and was not pleased with the slowdown in performance.
	These can be dangerous to turn off, especially the $H.  For those who use MPW
	more than I, you probably want to make these settable from the command line,
	or use the MacApp debug or no-debug compile time variables.  }
{$PUSH}		{ Save the compiler state before we change it. }
{$D+}			{ Debugging labels on for the code here. }
{$R-}			{ No range checking to make things faster. }
{$OV-}			{ No overflow checking either. }
{$H-}			{ No handle checking to avoid compiler complaints on WITHs.  Be careful. }


{-------------------------------  Application  -------------------------------}

PROCEDURE TFracAppApplication.IFracAppApplication(itsMainFileType: OSType);

VAR		I:						Integer;

BEGIN
	gStaggerCount := 0;
	IApplication(itsMainFileType);
	fIdlePriority := 1;						{ say we need Idle time calls to TApp.DoIdle }
	gRealMin := 0;							{ must be set to empty to start with. }
	gRealMax := 0;							{ so we do normal document open. }
END;		{ TFracAppApplication.IFracAppApplication }


	{ OK, this is where a new document gets created.  This does the init for the
		document object itself.  After it is done, the view and window can
		be created, relying upon the data in the document. }
FUNCTION  TFracAppApplication.DoMakeDocument(itsCmdNumber: CmdNumber):
		TDocument; OVERRIDE;

VAR		aFracAppDocument:	TFracAppDocument;

BEGIN
	{ Allocate and initialize the document}
	New(aFracAppDocument);
	FailNil(aFracAppDocument);

	{ Now initialize the document fields, and set up the global state of the fractal
		to a default set of the starting fractal. }
	aFracAppDocument.IFracAppDocument;

	{ We successfully created a document so we can return the document object for
		use by the application. }
	DoMakeDocument := aFracAppDocument;

END;	{ TFracAppApplication.DoMakeDocument }


	{ Performs Idle time processing for the application.  This will do the
		fractal calculation during the idle times.  It will allow each open
		document a chance to calculate.  The CalcCity is a method owned by
		each document that will get a call from the ForAllDocumentsDo.
		The documents don't use the DoIdle routine since we want each
		open document to get time, not just the one in the target chain. }
PROCEDURE  TFracAppApplication.DoIdle (phase: IdlePhase); OVERRIDE;

		{ Give each document some CPU time. }
	PROCEDURE DoFractalCalc(aDocument: TFracAppDocument);

	BEGIN
		aDocument.CalcCity;						{ give the document its time to calc. }
	END;

BEGIN
		{ Send the message to each open document to calculate the next pixel. }
	IF phase = IdleContinue  THEN  ForAllDocumentsDo (DoFractalCalc);
END;		{ TFracAppApplication.DoIdle }



{-------------------------------  Document  -------------------------------}

	{ An auxiliary method to set up the step constants for the fractal calculation.
		It is external since we need to set up the constants when we create a new
		fractal as a zoom in.   Sets up the width/height of fractal, the delta in each
		axis as a real number, and ensures that the starting min/max values for
		the figure are set to supply a 1:1 aspect ratio.  The step constants are
		zeroed to start the fractal anew.  Allocates no memory. }
PROCEDURE  TFracAppDocument.SetUpConstants;

BEGIN
	WITH fFracHeader  DO  BEGIN
		{ Set up the iterations by calculating up the step constants, and the
			edges of the view area in pixels. }

		plotWidth := (calcRect.Right - calcRect.Left);
		plotHeight := (calcRect.Bottom - calcRect.Top);
		deltaP := (realMax-realMin)/(plotHeight-1);
		deltaQ := (imagMax-imagMin)/(plotWidth-1);

		{ Force aspect ratio 1:1, making delta smallest of two.  This effectively grows
			one side or the other out, like rMax/iMax becoming bigger number. }
		IF deltaP > deltaQ  THEN  BEGIN
			deltaQ := deltaP;
			imagMax := deltaQ * (plotWidth-1) + imagMin; 	{ new maximum for q }
		END   { grow the q side }
		ELSE  BEGIN
			deltaP := deltaQ;
			realMax := deltaP * (plotHeight-1) + realMin; 		{ new maximum for p }
		END;  { grow the p side }

		{ Now start the counters at zero, as the edge of the area to calc. }
		curCol := 0;  curRow := 0;

		{ And the elapsed time is zero of course, since we are just starting. }
		elapsedTime := 0;
	END;		{ WITH fractalDocument }
END;		{ SetUpConstants }


	{ Utility method to build the offscreen Port that is used for
		the document data.  This happy fellow will allocate huge old hunks of Ram for
		the document.  This is done as a utility routine since we don't know in advance
		how big the port will be, and we want to make it as big as it was when the
		document was saved, reading it from the header.  If we are making a new
		document, the DoInitialState will call with the page rectangle.
		For this version we could have moved this back into the DoMakeDocuments
		where it belongs, but that would make each example even more different
		which is extra confusing. }

PROCEDURE  TFracAppDocument.BuildOffWorld (sizeOfDoc: Rect);

VAR		oldPerm: 			Boolean;
			dummy:				Boolean;
			docW, docH:		LongInt;
			fi: 					FailInfo;
			currPort:			GrafPtr;
			Erry:					OSErr;

		{ This is the error handler for when we get errors while making a new document,
			typically like running out of memory.  Since the Free method for the document
			will get called we don't have to chuck the things that normally get killed.
			Just set allocation back to normal (for the error message itself), the drawing
			environment back to normal and return.  }
	PROCEDURE DeathDocument (error: OSErr; message: LONGINT);

	BEGIN
		oldPerm := PermAllocation (oldPerm);	{ Set memory back to previous. }

		SetPort (currPort);
	END;

BEGIN
	GetPort(currPort);

	CatchFailures(fi, DeathDocument);			{ any failures, must be cleaned up. }

		{ The memory used creating the view must be out of permanent memory, it is too
			big.  Any failure to get it from permanent memory will invoke the error handler. }
	oldPerm := PermAllocation (TRUE);

		{ Let's set up the size of the rectangle we are using for the document. }
	docW := sizeOfDoc.right - sizeOfDoc.left;
	docH := sizeOfDoc.bottom - sizeOfDoc.top;

		{ Now try to set up the offscreen bitMap (color).  If we fail we have to split,
			and we might since we may not have 800K or more (basically a full screen
			worth, which is unlikely to be less than 800K) for the pixMap.  Each document
			on screen will have a full port for it.  Allocate a full screen size buffer in
			1 bit depth.  Also make it into a standard port so we can draw into it normally
			and use it as a source for CopyBits.  The buffer is (width DIV 8) giving bytes/row;
			then that times height to give total bytes required for the buffer }
	fBigBuff := NewPtr (docW * docH DIV 8);
	FailMemError;												{ couldn't get it we die. }

		{ The port is simply an interface for drawing.
			Allocate a port record on the heap as a pointer.  (permanent memory useage).  We
			allow the port to come out of temporary memory since it will blow up (most
			unfriendly) if it cannot do it.  After it lives, though we want to check if we
			have no more reserve, and if so we must bag this document. }
	fDrawingPort := GrafPtr( NewPtr (SizeOf (GrafPort)) );	{ address of Graf Port record. }
	FailNil (fDrawingPort);								{ didnÕt get it, means we die. }
	
		{ Now the world is created, put memory allocation back to temporary, so that the
			QD pieces can come out of temp memory as well.  No more permanent blocks are
			allocated by us, except for the port, which cannot fail or we die.  }
	dummy := PermAllocation (FALSE);

	OpenPort (fDrawingPort);								{ make a new port offscreen. }
	FailNoReserve;												{ Make reserve, die if we can't }

		{ QuickDraw is most obnoxious about making a port that is bigger than the screen,
			so we need to modify the visRgn to make it as big as our full page document.  It is
			OK to change this ports visRgn since we own it offscreen. }
	RectRgn(fDrawingPort^.visRgn, sizeOfDoc);

		{ Go whap on the other pieces of the port record to set it up to be offscreen. }
	fDrawingPort^.portBits.baseAddr := fBigBuff;
	fDrawingPort^.portBits.bounds := sizeOfDoc;
	fDrawingPort^.portBits.rowBytes := sizeOfDoc.right DIV 8;
	fDrawingPort^.portRect := sizeOfDoc;

		{ Clear the error handler chain, we don't make any more dangerous requests. }
	Success (fi);


		{ Set the memory allocation to what we started with. }
	oldPerm := PermAllocation (oldPerm);

		{ Now we have the offscreen PixMap, we need to initialize it to white. }
	SetPort (fDrawingPort);
	EraseRect (sizeOfDoc);				{ clear the bits. }

		{ We are done drawing and stuff for now, so set the port back to where it was. }
	SetPort (currPort);
END;	{ BuildOffWorld }


	{ Init for the FracAppDocument itself.  This sets up the Document object. }
PROCEDURE  	TFracAppDocument.IFracAppDocument;

VAR		dummyTime:		LongInt;		{ for picky compiler }

BEGIN
	{ Set up failure mechanism in case IDocument fails}
	IDocument(kFileType, kSignature, kUsesDataFork,
			NOT kUsesRsrcFork, NOT kDataOpen, NOT kRsrcOpen);

	{ Set the time in our starting time variable in case we are still calculating.
		Temp var is to make the picky compiler not get worried about the var
		parameter.  This routine can't move memory anyway, but it won't allow
		this use. }
	GetDateTime (dummyTime);
	fStartTime := dummyTime;

	fBigBuff := NIL;
	fDrawingPort := NIL;									{ set up in case we fail in here. }
END;	{ TFracAppDocument.IFracAppDocument }


	{ Does the work for a New operation, where we start with a new fractal
		that doesn't have any stored data.  This is to set up the view with no
		data and set up the fractal coordinates to the default.  It will use the size
		of the  main screen to make a new document, and create the offscreen
		world to match.  If the global variable of gRealMin and gRealMax are both
		nonzero, then we want to use the global state being passed us by the
		New Fractal handler.  This is for the zoom in. }
PROCEDURE TFracAppDocument.DoInitialState;  OVERRIDE;

BEGIN
	WITH  fFracHeader  DO BEGIN
			{ Start by filling in the fields that never change. }
		fType := kSignature;			{ creator of these documents. }
		hdrId := INTEGER ('FA');		{ ID of the file, different from other PICT files. }
		version := 1;						{ version 1 files. 0 was old MandibleJug docs. }

		done := FALSE;					{ not done, starting brand new document. }

		{ We start from scratch.  This is the standard set of coordinates to start
			the default Mandelbrot set.
			Set up the coordinates to do, saving state in header vars. }
		realMin := -2.5;  realMax := 1.5;
		imagMin := -1.5;  imagMax := 1.5;

			{ If we are supposed to do a zoom in, use those numbers instead. }
		IF (gRealMin <> 0) AND (gRealMax <> 0)  THEN BEGIN
			realMin := gRealMin;		realMax := gRealMax;
			imagMin := gImagMin;	imagMax := gImagMax;
		END;

			{ Set the fractal rectangle to be the full page size at 300 dpi. }
		SetRect(calcRect, 0, 0, kRectRight, kRectBottom);

	END;		{ With FracHeader }

		{ Clear the state of the globals so any new documents will not be zoom in types. }
	gRealMin := 0;		gRealMax := 0;

		{ Build the initial state of the document offscreen gDevice & port }
	BuildOffWorld (fFracHeader.calcRect);

		{ Set up the rest of the constants that are used in the fractal, including
			the deltas in each axis and the step constants for stepping through
			each point in the fractal plane. }
	SetUpConstants;
END;		{ TFracAppDocument.DoInitialState }


PROCEDURE TFracAppDocument.DoMakeViews(forPrinting: BOOLEAN); OVERRIDE;

VAR		aFracAppView:	TFracAppView;
			fullRect:				Rect;
			aFracAppHandler: TFracAppPrintHandler;

BEGIN
		{ Create a new view (failing if we can't), get a rectangle with
			the appropriate extent, and initialize the view. }
	New(aFracAppView);
	FailNil(aFracAppView);

		{ Initialize the view for use as a drawing environment.  The view rectangle
			is one fourth the size of the full page, since the screen is roughly one
			fourth the printer resolution.  This will produce the scaling down from
			300 dpi to screen res during update events. }
	SetRect (fullRect, 0, 0, kRectRight DIV 4, kRectBottom DIV 4);
	aFracAppView.IFracAppView (SELF, fullRect);

		{save a reference to the view in a TFracAppDocument field, for use
			by DoMakeWindows}
	fFracAppView := aFracAppView;

		{ Initialize our special print handler object so we can print.  This print
			handler will be the target of the print operation. }
	New(aFracAppHandler);
	FailNIL(aFracAppHandler);
	aFracAppHandler.IFracAppPrintHandler(aFracAppView);

END;	{ TFracAppDocument.DoMakeViews }


PROCEDURE TFracAppDocument.DoMakeWindows; OVERRIDE;

VAR		aWindow:	TWindow;

BEGIN
		{ Gets window definition from resource file; the window is to have both horizontal
			and vertical scrollbars, and is to have my 'fFracAppView' installed in it;
			NewSimpleWindow will exit via the failure mechanism if allocation fails.
			There is a palette associated with this window by Resource Id, so it will
			automatically get used when the window is created. }
	aWindow := NewSimpleWindow(kFracAppWindowID, NOT kDialogWindow,
					kWantHScrollBar, kWantVScrollBar, fFracAppView);

	SimpleStagger(aWindow, kStaggerAmount, kStaggerAmount, gStaggerCount);
END;	{ TFracAppDocument.DoMakeWindows }


	{ This routine will size the current image as it goes to the disk.  It won't actually
		save any data or anything, but will merely watch the bytes go by keeping track
		of how many go by.  The size is used by DoNeedDiskSpace. }
PROCEDURE  PictSizer (dPointer: Ptr; nextHunk: Integer);

BEGIN
	gPictSize := gPictSize + nextHunk;
END;


	{ Routine to find out how much disk space will be required to save the data.
		This does not call the Inherited DoNeedDiskSpace since we don't support
		printing info here.  The routine will replace the PutPicProc of the port
		with our PictSizer routine.  When the picture is created here, no bytes
		will actually be allocated or saved, we will just watch it go by and
		save off the size in the global variable.  That value is returned
		as the expected document size. }
PROCEDURE TFracAppDocument.DoNeedDiskSpace(VAR dataForkBytes,
			rsrcForkBytes: LONGINT);   OVERRIDE;

VAR		picPort:			GrafPtr;
			currPort:			GrafPtr;
			newGrafs:			QDProcs;
			oldProcs:			QDProcsPtr;

BEGIN
	{ Create a picture Item itself, by opening the picture and doing the CopyBits
		operation to the same port.  That picture will then be packed using the
		normal packing operation of the Mac.  That block is then the data to be
		written to the file. }

	GetPort (currPort);

	picPort := fDrawingPort;						{ the pointer to our port. }
	SetPort (picPort);											{ set there to do pict saving. }

		{ Save the pointer to the current CGrafProcs }
	oldProcs := thePort^.grafProcs;

		{ Set our GrafProc record up to have the standard pieces. }
	SetStdProcs(newGrafs);

		{ Change the port to use those GrafProcs instead. }
	thePort^.grafProcs := @newGrafs;

		{ We are in our offscreen port.  Change the GrafProc pointer for picture saving. }
	newGrafs.putPicProc := @PictSizer;

		{ Init the size of the pict we are going to save.  Start with picture header. }
	gPictSize := SIZEOF (Picture);

		{ The current gDevice is our offscreen device.  Now go ahead and open the picture
			and build it in RAM.  We would have done this by slices before, but the newer
			systems have a patch for playing back pictures that minimize the RAM hit, so
			we don't have to worry about the full screen CopyBits here. }
	WITH  picPort^  DO BEGIN
		gPictHandle := OpenPicture (portRect);

				{ copy all of the image to itself, in an open picture it saves the bits. }
			CopyBits (portBits, portBits, portRect, portRect, srcCopy, NIL);

		ClosePicture;						{ the picture is created, and packed. }
	END;		{ with picPort^ }


		{ Done saving the size of the picture itself.  Now set the GrafProcs back to normal. }
	thePort^.grafProcs := oldProcs;

		{ Dispose the pict handle, we didn't actually make anything there. }
	KillPicture (gPictHandle);
	gPictHandle := NIL;

		{ Set the drawing device back where it belongs, in case of error, we get right device. }
	SetPort (currPort);

		{ The picture has been sized.  Now add that in to the total size the file will use on
			disk, include the header for the file, plus the number of bytes in actual PICT. }
	dataForkBytes := dataForkBytes + gPictSize + kPICTHeaderSize;
END;		{ DoNeedDiskSpace }


	{ This routine will save the current image as it is created.  As the data requests
		go by that data will be written to the file.  The data is being created by the
		OpenPicture/CopyBits in DoWrite, this is the bottleneck for that operation.
		Any errors found while doing this will make us skip any further requests
		to write data to the disk.  No memory is allocated.   Communication with
		DoWrite is done through globals, since bottlenecks must be at the main
		level.  The bottleneck must also keep track of how many bytes are written,
		so that the header on the picture can be fixed up to be correct.  This must
		be done to avoid creating bogus pictures.  The picSize field of the handle
		must be updated continuously so that when the picture is done, the ClosePicture
		can create a valid picture.  The check for the NIL handle is to handle the
		problem of when the OpenPicture is called.  The proc gets called before
		the handle is valid.  Be very careful of these bottleneck things, it is
		easy to run into problems that are very hard to figure out.  QuickDraw
		has no facilities to give you info when things go wrong so it makes it
		a bit tougher. }
PROCEDURE  PictWriter (dPointer: Ptr; nextHunk: Integer);

VAR	longHunk:	LongInt;

BEGIN
	IF gPictError = noErr THEN  BEGIN
		longHunk := nextHunk;
		gPictError := FSWrite(gPictRefNum, longHunk, dPointer);
		gPictSize := gPictSize + longHunk;
		IF gPictHandle <> NIL THEN  gPictHandle^^.picSize := LoWord (gPictSize);
	END;
END;


	{ Write the data calculated into the document to the file.  This will make it a real
		PICT file.  It writes the header first, then the PICT data.  This is so that it
		will still be a normal PICT file and can be used by other programs.
		The file will be saved using QuickDraw Bottlenecks for the PutPicProc.
		As the data requests go by, they will be written to the file, using the
		PictWriter routine. }
PROCEDURE TFracAppDocument.DoWrite(aRefNum: INTEGER; makingCopy: BOOLEAN);
			OVERRIDE;

VAR		recSize:			LongInt;
			fi: 					FailInfo;
			picPort:			GrafPtr;
			currPort:			GrafPtr;
			newGrafs:			QDProcs;
			oldProcs:			QDProcsPtr;

	PROCEDURE DeathWrite (error: OSErr; message: LONGINT);
	BEGIN
		IF gPictHandle <> NIL THEN  	KillPicture (gPictHandle);
		gPictHandle := NIL;

		thePort^.grafProcs := oldProcs;
		SetPort (currPort);
	END;

BEGIN
		{ We have legit data in our document, set the mark in the file to be at the front. }
	FailOSErr ( SetFPos (aRefNum, fsFromStart, 0) );

		{ Write the FracHeader to the file, it includes the pertinent details about
			the fractal including the global state for it. }
	recSize := SIZEOF (FracRecord);   						{ our header on fractal files. }
	FailOSErr ( FSWrite (aRefNum, recSize, @fFracHeader) );

		{ Now we need to write the picture data itself out to the file, after we set the
			mark to be after the entire header.  Make sure the file is that big before we do it.
			Included in this set is the header of the picture itself, the 10 bytes that
			include the rectangle.  Those bytes will be updated after the picture is
			written. }
	FailOSErr ( SetEOF (aRefNum, kPICTHeaderSize+SIZEOF (Picture) ) );
	FailOSErr ( SetFPos (aRefNum, fsFromStart, kPICTHeaderSize+SIZEOF (Picture) ) );

		{ The file is all set up to go.  We now want to replace the QuickDraw bottleneck
			and create the actual Picture data. }
	GetPort (currPort);

		{ If the write of the picture header fails, we want to dispose the handle allocated. }
	CatchFailures(fi, DeathWrite);

		{ Move over to the offscreen port/device. }
	picPort := fDrawingPort;					{ the pointer to our port. }
	SetPort (picPort);											{ set there to do pict saving. }

		{ Save the pointer to the current CGrafProcs }
	oldProcs := thePort^.grafProcs;

		{ Set our GrafProc record up to have the standard pieces. }
	SetStdProcs(newGrafs);

		{ Change the port to use those GrafProcs instead. }
	thePort^.grafProcs := @newGrafs;

		{ We are in our offscreen port.  Change the GrafProc pointer for picture saving. }
	newGrafs.putPicProc := @PictWriter;

		{ Tell PictWriter what file to write to, and start the pic size including the
			picture header.  Start all the pieces off the right way. }
	gPictRefNum := aRefNum;
	gPictSize := SIZEOF(Picture);
	gPictError := noErr;
	gPictHandle := NIL;

		{ Actually open the picture and do the CopyBits in order to process the picture.
			The data will be written by PictWriter as it is called by QuickDraw. }
	WITH  picPort^  DO BEGIN
		gPictHandle := OpenPicture (portRect);
			ClipRect(portRect);			{ Make it a happier picture. }

				{ copy all of the image to itself, in an open picture it saves the bits. }
			CopyBits (portBits, portBits, portRect, portRect, srcCopy, NIL);

		ClosePicture;						{ the picture is created, and packed. }
	END;		{ with picPort^ }

		{ Now check for errors during the write operation.  The gPictError field will be
			nonzero if we failed during the operation. }
	FailOSErr (gPictError);

		{ Move back to front of file and write the valid picture info to file. }
	FailOSErr ( SetFPos (aRefNum, fsFromStart, kPICTHeaderSize) );
	recSize := SIZEOF(Picture);
	FailOSErr (FSWrite(aRefNum, recSize, Ptr(gPictHandle^)));

		{ Done saving the data of the picture itself.  Now set the GrafProcs back to normal. }
	thePort^.grafProcs := oldProcs;

		{ Dispose the pict handle, we didn't actually make anything there. }
	KillPicture (gPictHandle);
	gPictHandle := NIL;

		{ Set the drawing device back where it belongs, in case of error, we get right device. }
	SetPort (currPort);

		{ If we lived through it, clear error handler. }
	Success (fi);
END;			{ TFracAppDocument.DoWrite }


	{ The bottleneck routine to read the picture from the disk.  This will read the
		data required, and pass it along to the unpacker.  This makes it possible to
		avoid using any RAM for the actual reading part, as it is being played back
		into the offscreen device.  Error handling is somewhat tricky, since we
		need to force the picture to finish, and there isn't a really good way to
		do this.  The desired attempt here is to pass back a picture is finished
		opcode ($00FF) so we can get back to our code to handle the error.  This is
		better than no error recovery, but is not guaranteed to work. }
PROCEDURE  PictReader (dPointer: Ptr; nextHunk: Integer);

VAR	longHunk:	LongInt;
		I:				Integer;

BEGIN
	IF gPictError = noErr THEN  BEGIN
		longHunk := nextHunk;
		gPictError := FSRead(gPictRefNum, longHunk, dPointer);
	END
	ELSE			{ handle the error situation by passing back $00FF as the data.? }
		FOR I := 1 to nextHunk  DO BEGIN
			IF ODD (I) THEN  dPointer^ := $FF
			ELSE  dPointer^ := $00;
			dPointer := PTR (ORD4(dPointer) + 1);
		END;
END;


	{ Routine to read the data from the data fork of the file into our document so it
		can be displayed.  The quickdraw bottleneck will be replaced with the
		PictReader routine, making it read the data from the disk as the picture
		requests more data.  This obviates the need for an extra handle that is
		used to play back the picture.  This is done since that extra handle can
		be on the order of 400K, memory we may not have available. }
PROCEDURE TFracAppDocument.DoRead(aRefNum: INTEGER; rsrcExists,
												  forPrinting: BOOLEAN);  OVERRIDE;

VAR		recSize:			LongInt;
			fi: 					FailInfo;
			currPort:			GrafPtr;
			newGrafs:			QDProcs;
			oldProcs:			QDProcsPtr;

	PROCEDURE DeathRead (error: OSErr; message: LONGINT);
	BEGIN
		IF gPictHandle <> NIL THEN  KillPicture (gPictHandle);
		gPictHandle := NIL;
	END;

BEGIN
		{ The file is open already, we just have to read the data out of it.  The first thing
			to read is the header we use to describe a fractal.  If we get an error
			here we need to split since we should always have at least a header.  The fractal
			header is the global state for the document.  We just read it into the record
			and use it from there. }
	FailOSErr ( SetFPos (aRefNum, fsFromStart, 0) );	{ starts at first byte of file. }
	recSize := SIZEOF (FracRecord);   								{ size of header on fractal files. }
	FailOSErr ( FSRead (aRefNum, recSize, @fFracHeader) );

		{ We have the header for the PICT file.  Now we need to be sure that it is a fractal
			document, and not something we can't use.  Check the header to be sure, and if
			not right, error out with a good alert message (using a standard MacApp errcode). }
	IF fFracHeader.fType <> kSignature  THEN  FailOSErr (errNotMyType);

		{ We have the data from the header, go ahead and set up an offscreen world for this
			document, using the header rectangle. }
	BuildOffWorld (fFracHeader.calcRect);

		{ Make sure the file position is right at the start of the picture in the file. }
	FailOSErr ( SetFPos (aRefNum, fsFromStart, kPICTHeaderSize) );

		{ Allocate a small handle that will be used as the Pict handle for drawing from
			the disk.  This is just the picture header. }
	gPictHandle := PicHandle (NewHandle (SIZEOF(Picture)));
	FailNil (gPictHandle);

		{ If the read of the picture header fails, we want to dispose the handle allocated. }
	CatchFailures(fi, DeathRead);

		{ Tell PictReader what file to read from. }
	gPictRefNum := aRefNum;
	gPictError := noErr;

		{ Now fill in the picture header itself, using the data from the disk. }
	recSize := SIZEOF(Picture);
	gPictError := FSRead(aRefNum, recSize, Ptr (gPictHandle^));
	FailOSErr (gPictError);

		{ That is the only call we canÕt recover from immediately, the rest of the
			routine is not easy to recover from, so we wonÕt go through DeathRead. }
	Success (fi);
	
		{ The file position is right at the beginning of the picture data, so we can just
			install the bottleneck and call DrawPicture to fill our offscreen gDevice
			with the data that was saved.  Set to that port and gDevice for playback. }
	GetPort (currPort);

	SetPort (fDrawingPort);

		{ Save the pointer to the current CGrafProcs }
	oldProcs := thePort^.grafProcs;

		{ Set our GrafProc record up to have the standard pieces. }
	SetStdProcs(newGrafs);

		{ Change the port to use those GrafProcs instead. }
	thePort^.grafProcs := @newGrafs;

		{ We are in our offscreen port.  Change the GrafProc pointer for picture reading. }
	newGrafs.getPicProc := @PictReader;

		{ Now we have the buffer and the offscreen port.  We can draw the picture that
			will be read out of the file into this port in order to init the port for later use in
			updating the window.  We are already set to draw in the offscreen port.  Do the
			DrawPicture to have PictReader read the data out of the file while it is being
			played into the offscreen Port. }
	DrawPicture(gPictHandle, gPictHandle^^.picFrame);

		{ Done reading the data of the picture itself.  Now set the GrafProcs back to normal. }
	thePort^.grafProcs := oldProcs;

		{ Bag the handle we made for playing back the picture. }
	KillPicture (gPictHandle);
	gPictHandle := NIL;

		{ Set back to the normal drawing environment. }
	SetPort (currPort);

		{ If we had an error while reading the data, we must error out. }
	FailOSErr (gPictError);
END;		{ TFracAppDocument.DoRead }


	{ This is typically used in a Revert case which is not really meaningful here, but
		the structure is the same so we use it anyway.  Frees the data associated with
		a document, that is strictly program data, not MacApp data. }
PROCEDURE TFracAppDocument.FreeData; OVERRIDE;

BEGIN
		{ Kill the bits for the offscreen bitMap if they were allocated. }
	IF fBigBuff <> NIL  THEN  DisposPtr (fBigBuff);
		{ Close the port: remove from portList, kill visRgn and clipRgn, kill the penPixPat
			and fill PixPat and back PixPat, kill PixMap handle, kill grafVars handle. }
	IF fDrawingPort <> NIL  THEN  BEGIN
		ClosePort (fDrawingPort);
		DisposPtr (Ptr (fDrawingPort) );
	END;
END;	{ TFracAppDocument.FreeData }


	{ Free method for the documents themselves.  We need to override so that we
		can throw away the data object that was read in from the disk if it exists.
		Also chuck the buffer and port used for the document data. }
PROCEDURE TFracAppDocument.Free; OVERRIDE;

BEGIN
	FreeData;

	INHERITED Free;
END;	{ TFracAppDocument.Free }


	{ The procedure to do the idle time processing in the document.  This will do the
		entire fractal calculation so as to be able to do it in the background.  It
		does it one pixel at a time to avoid any hit on performance for the
		foreground application.  This is called in response to the DoIdle for the
		application.  The fIdlePriority is not set for this method, so it won't get
		time except when the application calls specifically.  It is done this way
		since otherwise the target chain would need to have each document in
		the list, which is not desireable for other event handling.  Notably the
		time keeper in here is not too accurate.  Each pixel takes less than a
		tick to calculate, making it a bit tougher.  A way to make it more
		accurate would be to figure out the maximum time for a full black
		document, and divide by the number of pixels in the screen and the
		number of loops.  That number (in microseconds) could be added each
		time through the calculation loop to give a more accurate timestamp.
		This would be wrong if the clock changes, so perhaps it should use the
		low memory TimeDBRA value as units instead.
		This is left as an exercise for the reader. }
PROCEDURE  TFracAppDocument.CalcCity;

CONST	M				= 100;					{ this decides what 'infinity' is.  If value less than this, loop. }
			K				= 195;					{ number of times to iterate while waiting for infinity. }

VAR		currTime:			LongInt;		{ temp var for time check. }

			x,y,x1,y1:			Extended;		{ for interim values of current point. }
			Po,Qo:				Extended;

			kol:					Integer;		{ color we are currently on. }
			r:						Extended;		{ 'distance' from root. }
			currPort:			GrafPtr;
			drawRect:			Rect;			{ for updating the screen as we calculate. }

BEGIN
		{ Calculate the fractal as we go.  Do next pixel here, based on the state saved
			in the document object.  When done, the variables are updated to go to the
			next location to do.  It sets the pixel in the offscreen port to be whatever
			we calculate it to be.  The buffer will be copied to the screen at update time.
			The global state is saved in the FracHeader record in the document object.
			That state is saved across the use of a document, so it will always be right. }

		{ If we are done, or not started, we can split. }
	IF  fFracHeader.done THEN  Exit(CalcCity);

	GetPort (currPort);
	SetPort (fDrawingPort);		{ draw in offscreen guy. }

	{ Now do the calculation to determine the color of the pixel at the
		current location.  Uses the header saved state. }

	With  fFracHeader  DO BEGIN
(*		x := realMin + curCol * deltaP;					{ Use these for a Julia set calculation. }
		y := imagMin + curRow * deltaQ;
		kol := 0;
		Po := -0.39054;
		Qo := 0.58679;
*)
		Po := realMin + curRow * deltaP;				{ next starting point  }
		Qo := imagMin + curCol * deltaQ;
		kol := 0;
		x := 0;														{ Mandel set starts with 0 always. }
		y := 0;														{ For Julia set you start with previous number. }
	END;		{ With }

	REPEAT
		{ the following is for y = X^2 + C for imaginary numbers.
		pt1 = x + yi,  C = Po + Qoi,  in pt2 := pt1^2 + C  }

		x1 := x*x - y*y + Po;
		y1 :=2*x*y + Qo;

		kol := kol + 1;
		x := x1;  y := y1;

		r := x1*x1 + y1*y1;
	UNTIL  (r > M) OR (kol > K);				{ Until 'distance' > our infinity, or out of colors. }

	IF kol <= K THEN								{ Decide which zebra color to use. }
		IF ODD (kol) THEN PenPat (Black)
		ELSE  PenPat (White)
	ELSE    												{ must be kol > K, ran out of colors. }
		PenPat (Black);

		{ Move to the pixel we calculated for, then draw the pixel in right color.  This
			could be done by setting the bytes in pixel map directly, since we own the
			PixMap and the buffer. }
	MoveTo (fFracHeader.curCol, fFracHeader.curRow);
	Line (0,0);       									 { draw that 'pixel' in the right color }


		{ up the counters to the next pixel location to do. }
	WITH  fFracHeader  DO BEGIN
		curCol := curCol + 1;									{ up the column count. }
		drawRect := thePort^.portRect;					{ in case we finished a line. }
		IF curCol >= plotWidth  THEN  BEGIN			{ did we run off end of row? }
				{ Have the line just calculated drawn to window.  DIV 4 since we are
					4 times bigger offscreen. }
			drawRect.top := curRow DIV 4;
			drawRect.bottom := (curRow+1) DIV 4;

			curCol := 0;											{ start on the next row. }
			curRow := curRow + 1;							{ and up the counter of the next row to do. }
		END;  { start at next row. }

	END;  { with  fFracHeader }

		{ Check if we are done, and if so, set the flag to stop calculations.  Set the
			elapsed time counter in the header. }
	IF fFracHeader.curRow >= fFracHeader.plotHeight  THEN  BEGIN
		fFracHeader.done := TRUE;
		GetDateTime(currTime);
		fFracHeader.elapsedTime := currTime - fStartTime;
	END;

	IF NOT EqualRect (thePort^.portRect, drawRect) THEN
		fFracAppView.InvalidRect (drawRect);

	SetPort (currPort);

	{ Now we have changed another point in the document.  We need to mark it as
		changed so we can save the document. }
	fChangeCount := fChangeCount + 1;

END;		{ TFracAppDocument.DoIdle }


{-------------------------------  View  -------------------------------}

	{ Initialize the view, basically set up the view object and clear the selection. }
PROCEDURE 	TFracAppView.IFracAppView (itsDocument: TFracAppDocument;
		sizeOfView: Rect);

BEGIN
	fSelectionRect := gZeroRect;					{ no selection to start with. }
	fFracAppDocument := itsDocument;		{ save off parent document for convenience. }

		{ This view will be the full size of the screen since we have an offscreen
			bitMap as the view.  This will be clipped to fit the frame of the window.
			There is no parent view, and the horizontal and vertical are fixed.  The
			selection is to be shown, and is initially off. }
	IView(NIL, itsDocument, sizeOfView, sizeFixed, sizeFixed, TRUE, hlOff);

END;		{ TFracAppView.IFracAppView }


	{ Our routine to do the drawing of the fractal.  This is the display routine
		to take the data out of the offscreen buffer and whip it up to the window,
		as the current view.  The view is scaled down to screen res, giving an
		image that is close to page size, but with less resolution. }
PROCEDURE TFracAppView.Draw(area: Rect); OVERRIDE;

VAR		srcSize: REct;

BEGIN
	srcSize.top := area.top * 4;
	srcSize.left := area.left * 4;
	srcSize.bottom := area.bottom * 4;
	srcSize.right := area.right * 4 ;
	CopyBits ( fFracAppDocument.fDrawingPort^.portBits,
			thePort^.portBits, srcSize, area, srcCopy, Nil);
END;		{ TFracAppView.Draw }


	{ Handle the menu choice for New Fractal out of the Fractal Menu.  This makes a new
		Fractal based on the current selection.  It does it by calling on the application
		object to make a new document.  The communication to the DoInitialState is
		through the global variables. }
FUNCTION TFracAppView.DoMenuCommand(aCmdNumber: CmdNumber): TCommand;  OVERRIDE;

VAR		oldDeltaP,
			oldDeltaQ,
			oldRealMin,
			oldImagMin:	Extended;					{ for figuring new fractal area. }

BEGIN
	{ Assume that we have no command to return, since none of our commands currently
		change the document. }
	DoMenuCommand := gNoChanges;			{ no command object returned. }

	{ Case off on the various menus.  Currently we have the new fractal item.
		Any out of that list are handled by Mr. MacApp and we pass it on. }
	CASE  aCmdNumber  OF

		kNewFractal:
			{ If the option chosen was the New Fractal item, then we need to start
				up a fresh one based on the selection rectangle.  This new fractal is
				based on parts of the old one, since it is
				a zoom in operation.  We make a new document/window/view
				as if it were a New operation.  We then change the fields we need to in
				that document to make it start calculating based on the selection from
				the current view. }

			BEGIN
				{ Make a new document and initialize it to the base state.  If we fail in
					opening it, we won't return here, the failure handler will kill it.  We
					have nothing else to dispose of, so we don't make a CatchSignals here.
					This will come out of permanent memory.  The aCmdNumber is so that
					the new document knows it came from a zoom in operation.  Since this
					is somewhat funky, we communicate to the other part of the program
					with the global variables.  If nonzero, the code that makes a
					new document will know to use these numbers in order to do the zoom.
					This is less than completely desireable, but there are no good places
					to override in order to get both the selection rectangle and the new
					document objects. }

					{ The basic fractal has been set up.  We now need to change the calculation
						area based on the current selection, in order to effect the zoom in. }
				WITH  fFracAppDocument.fFracHeader DO BEGIN
					oldDeltaP := deltaP;							{ from SELF, the old document. }
					oldDeltaQ := deltaQ;
					oldRealMin := realMin;
					oldImagMin := imagMin;
				END;

				{ calculate new min/max for real and imaginary parts based on how far
					into the old fractal plane we were.  This is an extended calculation
					since our plane is in extendeds.  We get the new locations of min and
					max, and save them off.  We reset the deltaP or Q with SetUpConstants,
					in order to force a 1:1 ratio, but we need all sides to determine
					which one to force.  Scale up the selection area by 4 since it is in
					screen res, not page res. }
				gRealMin := oldRealMin + oldDeltaP * (fSelectionRect.top*4);
				gImagMin := oldImagMin + oldDeltaQ * (fSelectionRect.left*4);
				gRealMax := oldRealMin + oldDeltaP * (fSelectionRect.bottom*4);
				gImagMax := oldImagMin + oldDeltaQ * (fSelectionRect.right*4);

				gApplication.OpenNew (aCmdNumber);
			END;

		OTHERWISE
			DoMenuCommand := INHERITED  DoMenuCommand (aCmdNumber);	{ next guy in chain. }
	END;		{ CASE on aCmdNumber }

END;	{ TFracAppView.DoMenuCommand }


	{ Set up the New Fractal menus choice in Fractal Menu, based on selection. }
PROCEDURE TFracAppView.DoSetupMenus;  OVERRIDE;

BEGIN
	INHERITED DoSetupMenus;							{ Do mainline stuff first. }

		{ If we have a non-zero selection, then we can enable the menu item to use
			it as the new fractal dimensions for this document. }
	Enable (kNewFractal, NOT EmptyRect (fSelectionRect));
END;	{ TFracAppView.DoSetupMenus }


	{ The way to handle mouse events in the content region of the view.  This will
		pass back the command object to handle tracking the mouse and creating a
		new selection in preparation for making a new fractal. }
FUNCTION  TFracAppView.DoMouseCommand(VAR downLocalPoint: Point;
						VAR info: EventInfo;  VAR hysteresis: Point): TCommand;	OVERRIDE;

VAR		tracker:	TAreaSelector;

BEGIN
	New(tracker);										{ make a new command object. }
	FailNIL(tracker);									{ no memory, trash out. }
	tracker.IAreaSelector(SELF, downLocalPoint); { Initialize the command object. }
	DoMouseCommand := tracker;				{ return it for later use. }
END;		{ TFracAppView.DoMouseCommand }


	{ Highlight the current selection rectangle if there is one.  This is drawn in scrCopy
		mode to make it stand out better when it is a final selection.  XOR is used for
		the rubberband, until mouseUp. }
PROCEDURE TFracAppView.DoHighLightSelection(fromHL, toHL: HLState); OVERRIDE;

VAR		selPatHandle:	PatHandle;

BEGIN
	IF toHL = hlOn THEN  BEGIN
		selPatHandle := GetPattern(kSelPattern);	{ get the pattern we use. }
		IF selPatHandle <> NIL  THEN 						{ If pattern available, use it. }
			PenPat (selPatHandle^^);							{ set pen pattern to our selection kind. }
		PenMode(srcCopy);										{ copy mode on pattern selection. }

			{ We have a selection, so go ahead and draw the selection rectangle. }
		FrameRect (fSelectionRect);						{ outline the frame of selection. }
	END;		{ highlight turned on. }

		{ Turning off the highlight, we need to remove the traces of the selection.
			To do this, redraw that rectangle. }
	IF toHL = hlOff THEN   Draw (fSelectionRect);	{ ReDraw it to clear selection. }
END;		{ TFracAppView.DoHighlightSelection }


{-------------------------------  Command  -------------------------------}

	{ Initialize the selector object itself.  Sets up the normal fields. }
PROCEDURE TAreaSelector.IAreaSelector(ownerView: TFracAppView; startPt: Point);

BEGIN
	ICommand(cMouseCommand);					{ initialize normal parts of command }

	fCausesChange := FALSE;							{ just selection, not changing document. }
	fCanUndo := FALSE;									{ therefore, no Undo of no change. }
	fConstrainsMouse := TRUE;						{ do the constrain to match to screen. }
	fOwnerView := ownerView;						{ save the view for use in tracking. }
END;	{ TAreaSelector.IAreaSelector }


	{ Track the mouse while the button is down.  This is overridden so we can leave
		the command object as not having changed, ie. so we can pass back the
		gNoChanges as the last step since this is not an undoable operation.  It
		doesn't change the view, so we don't need to DoIt or Commit. }
FUNCTION  TAreaSelector.TrackMouse(aTrackPhase: TrackPhase;
						VAR anchorPoint, previousPoint, nextPoint: Point;
						mouseDidMove: BOOLEAN): TCommand;  OVERRIDE;

VAR		selPatHandle:		PatHandle;

BEGIN
	TrackMouse := SELF;								{ Assume we are not in release phase. }

	CASE aTrackPhase OF
		trackPress:
		BEGIN
			fOwnerView.DoHighLightSelection (hlOn, hlOff);		{ turn off old selection if any. }
			fOwnerView.fSelectionRect := gZeroRect;				{ clear rect, there isn't one. }
		END;

		trackRelease:
		BEGIN
			Pt2Rect(anchorPoint, nextPoint, fOwnerView.fSelectionRect);
			fOwnerView.DoHighlightSelection (hlOff, hlOn);		{ leave on selection. }
			TrackMouse := gNoChanges;
		END;
	END;	{ Case on aTrackPhase }
END;	{ TAreaSelector.TrackMouse }


	{ Track the mouse giving the feedback of a different rectangle kind.  This is so
		we can use the selection pattern to give a preferred rectangle.  The selection
		pattern comes out of temporary memory so as to not fail needlessly. }
PROCEDURE TAreaSelector.TrackFeedback(anchorPoint, nextPoint: Point;
						turnItOn, mouseDidMove: BOOLEAN);  OVERRIDE;

VAR		selBoy:					Rect;
			selPatHandle:		PatHandle;

BEGIN
	IF mouseDidMove THEN
		BEGIN {the pen is already in patXOR mode, black, one wide}
			selPatHandle := GetPattern(kSelPattern);	{ get the pattern we use. }
			IF selPatHandle <> NIL  THEN						{ use our pattern if available. }
				PenPat (selPatHandle^^);							{ set pen pattern to our selection kind. }

			Pt2Rect(anchorPoint, nextPoint, selBoy);
			FrameRect(selBoy);
		END;
END;	{ TAreaSelector.TrackFeedback }


	{ Constrain the mouse to a rectangle that is the same proportion as the screen, so
		we can make the selection match better without having to guess at the length
		or width, or scaling the chosen rect to fit the screen.  Small piece chosen will
		blow up to fit easily.  This will make it easier to choose a selection that
		gives a 1:1 aspect ratio.  This also chooses which direction the mouse has
		moved, deciding which is larger in order to decide the direction to constrain. }
PROCEDURE TAreaSelector.TrackConstrain(anchorPoint, previousPoint: Point;
							VAR nextPoint: Point);  OVERRIDE;

VAR		newWidth, newHeight:		LongInt;
			mouseRatio, plotRatio:		Real;
			constrainRect:					Rect;

	PROCEDURE  ChangeWidth;
	BEGIN
		WITH  fOwnerView.fFracAppDocument.fFracHeader  DO BEGIN
				{ Get the new width as a positive number, a displacement that is constrained. }
			newWidth := Round (ABS (nextPoint.v - anchorPoint.v) * plotWidth / plotHeight);
				{ Decide which quadrant we are in, moving the right direction. }
			IF nextPoint.h < anchorPoint.h  THEN newWidth := -newWidth;
				{ Actually change the final point to pass back. }
			nextPoint.h := anchorPoint.h + newWidth;						{ add offset to get new pt. }
		END;
	END;

	PROCEDURE  ChangeHeight;
	BEGIN
		WITH  fOwnerView.fFracAppDocument.fFracHeader  DO BEGIN
			newHeight := Round (ABS (nextPoint.h - anchorPoint.h) * plotHeight / plotWidth);
			IF nextPoint.v < anchorPoint.v  THEN newHeight := -newHeight;
			nextPoint.v := anchorPoint.v + newHeight;					{ add offset to get new pt. }
		END;
	END;

	PROCEDURE  PinPoint;					{ Pin the rectangle to the edge of the view Rect. }
	BEGIN
		WITH  fOwnerView.fExtentRect  DO BEGIN
			SetRect(constrainRect, left, top, right, bottom);
			nextPoint := Point (PinRect(constrainRect, nextPoint));
		END;
	END;

BEGIN
	WITH  fOwnerView.fFracAppDocument.fFracHeader  DO BEGIN
		mouseRatio := ABS ((nextPoint.h - anchorPoint.h)/(nextPoint.v - anchorPoint.v));
		plotRatio := plotWidth/plotHeight;

			{ The deltaX, deltaY can be thought of as a rect too.  If the ratio of sides on
				that rect (width/height) is greater than the ratio of width/height of the
				plot rectangle, then we need to grow the height of the rect.  If it is less,
				we need to grow the width.  This is a ratio of sides to decide which way
				to grow.  We grow to make the new rect still touch the mouse position.
				It can be thought of as the rectangle being thicker than tall wanting to
				grow the tall part in a constrained way, and the corollary for the width. }
		IF mouseRatio > plotRatio  THEN BEGIN		{ constrain height to new value. }
				ChangeHeight;
				PinPoint;
				ChangeWidth;
			END
		ELSE		BEGIN		{ constrain width to new value. }
				ChangeWidth;
				PinPoint;
				ChangeHeight;
			END;
	END;	{ With }
END;	{ TAreaSelector.TrackConstrain }


{-------------------------------  TFracAppPrintHandler  --------------------------}

{ A print handler for doing 300 dpi printing, bypassing the standard print handler for
	it.  Uses the port created for calculating the fractal, and prints it using PrGeneral
	commands.  It is a print handler object so we get the fields and methods of the
	printing object.  Since it is an event handler, it will handle the menu events for
	the print operation, as well as enable their use. }

	{ Init the print handler, setting up the fields in the handler.  Also attach it to the
		view and document so it knows who to call to set up the printing stuff.  }
PROCEDURE 	TFracAppPrintHandler.IFracAppPrintHandler (itsView: TFracAppView);
BEGIN
	IPrintHandler(itsView);
	fOwningView := itsView;
	IF itsView <> NIL THEN  fView.AttachPrintHandler(SELF);
	itsView.fFracAppDocument.fDocPrintHandler := SELF;
END;

	{ Enable the printing commands that this object handles.  Enabled when there
		is a document open. }
PROCEDURE TFracAppPrintHandler.DoSetupPrintingMenus;  OVERRIDE;
BEGIN
	INHERITED DoSetupMenus;									{ Do mainline stuff first. }
	IF fView <> NIL  THEN  Enable (cPrint, TRUE);
END;

	{ Do the print operation.  This is the code to
		actually send the data to the print manager and set it up using the highest
		resolution that the printer supports.  It is general purpose and will work on
		any printer that supports PrGeneral.  Part of this code was stolen from the
		MacApp StdPrintHandler in order to conform with the techniques used there. 
		There is no good place to override the StdPrintHandler in order to be able to
		call PrGeneral, which is why we are replacing the entire handler. }
FUNCTION TFracAppPrintHandler.Print(itsCmdNumber: CmdNumber; 
					VAR proceed: BOOLEAN): TCommand; OVERRIDE;

VAR	fractalPort:		GrafPtr;
		thePrintRec:	THPrint;
		theGetRslBlk:	TGetRslBlk;
		theSetRslBlk:	TSetRslBlk;
		thePrintPort:	TPPrPort;
		thePrStats:		TPrStatus;
		fi: 					FailInfo;
		PrIsOpen:		Boolean;
		DocIsOpen:		Boolean;
		err:					OSErr;
		
	PROCEDURE DeathPrint (error: OSErr; message: LONGINT);

	BEGIN
		IF DocIsOpen THEN  PrCloseDoc(thePrintPort);
		
			{ Set the error code to what the print manager really wanted to say.  This is
				to avoid messages that have already been displayed by the Print Manager.  This
				is done after closing the document to be sure we have the final error result. }
		error := PrError;
		
		IF PrIsOpen THEN  PrClose;
		DisposHandle(Handle(thePrintRec));
		
			{ Some errors should be changed to a message more meaningful. }
		IF (error = fnfErr) OR (error = resFNotFound) THEN  
			error := errNoPrintDrvr;
		IF error = resNotFound  THEN
			error := kBadPrinter;
			
			{ If it was a user abort, we don't want to put up an alert. }
		IF error = iprAbort THEN
			Failure(noErr, 0);
		
			{Certain Print Manager errors should not result in any alert,
				since the Print Manager will have already put one up.}
		IF (error >= -8160) AND (error <= -8150) THEN
			Failure(0, msgPrintFailed);
			
			{Add the name of the document to the error alert to make it easy to read.}
		IF message = 0 THEN
			gErrorParm3 := fView.fDocument.fTitle;
		FailNewMessage(error, message, msgPrintFailed);
	END;	{ DeathPrint }

BEGIN
	Print := gNoChanges;											{ Not undoable operation. }
	proceed := FALSE;												{ If we get an error or cancel out. }

	CASE  itsCmdNumber  OF
		cPrint, cFinderPrint:  BEGIN
				{ They wish to print.  This is done by our special print handler, which overrides the
					standard one.  It will print after setting up the special resolution using the PrGeneral
					call. }
			fractalPort := fOwningView.fFracAppDocument.fDrawingPort;
			PrIsOpen := FALSE;
			DocIsOpen := FALSE;
			
			thePrintRec := THPrint(NewHandle(SIZEOF(TPrint)));
			FailNil(thePrintRec);

			CatchFailures(fi, DeathPrint);				{ any failures, must be cleaned up. }

			PrOpen;
			FailOSErr (PrError);							{ An error here is driver not found. }
			
			PrIsOpen := TRUE;									{ set our flag for error recovery. }

				{ Initialize the print record with default values since we don't supply a 
					page setup operation. }
			PrintDefault(thePrintRec);

				{ Get the possible resolutions for the current device... }
			theGetRslBlk.iOpCode := getRslDataOp;
			PrGeneral(@theGetRslBlk);
			FailOSErr (PrError);							{ An error here means driver doesn't have resource. }
				{ An error here means driver doesn't support resolutions.  Since there is no predefined
					error message for this, we will turn it into our own error code so that the right code
					from the 'errs' resource will map to the new STR# error message. }
			IF theGetRslBlk.iError <> noErr THEN FailOSErr (kBadPrinter);

				{ Set the device to the maximum possible resolution.  This is a little funky, since we
					assume that the highest resolution is the last element of the table.  This is valid
					currently and sensible, but not explicitly documented. }
			WITH theGetRslBlk, theSetRslBlk DO BEGIN
				theSetRslBlk.iOpCode := setRslOp;
				hPrint := thePrintRec;
				iXRsl := rgRslRec[iRslRecCnt].iXRsl;
				iYRsl := rgRslRec[iRslRecCnt].iYRsl;
			END;
			PrGeneral(@theSetRslBlk);
			FailOSErr (PrError);
			IF theSetRslBlk.iError <> noErr THEN FailOSErr (kBadPrinter);

				{ Call the job dialog to see what the user wants.  If they click OK, we continue.  If not,
					we error out with the noErr error code to clean up, but it won't show an alert.  This
					is done to avoid the confusing nested IF structures so common in printing code...}
			IF NOT PrJobDialog(thePrintRec) THEN Failure (noErr, 0);
			
			thePrintPort := PrOpenDoc(thePrintRec, NIL, NIL);
				{ Handle the special case of out of disk space, by failing with right message. }
			IF PrError =  -1 {iPrSavPFil} THEN Failure(errSpooling, 0);
			
			DocIsOpen := TRUE;								{ Set so error routine can close the right pieces. }

			PrOpenPage(thePrintPort, NIL);
			FailOSErr (PrError);

				{ Do the actual drawing to the printer port. }
			CopyBits (fractalPort^.portBits, thePrintPort^.GPort.portBits,
					fractalPort^.portRect, fractalPort^.portRect, srcCopy, NIL);
			FailOSErr (PrError);

			PrClosePage(thePrintPort);
				{ Handle the special case of out of disk space, by failing with right message. }
			IF PrError =  -1 {iPrSavPFil} THEN Failure(errSpooling, 0);

			PrCloseDoc(thePrintPort);
				{ Handle the special case of out of disk space, by failing with right message. }
			IF PrError =  -1 {iPrSavPFil} THEN Failure(errSpooling, 0);

			DocIsOpen := FALSE;

				{ Image the spool file if necessary. }
			IF  thePrintRec^^.prJob.bJDocLoop = bSpoolLoop  THEN
				PrPicFile(thePrintRec, NIL, NIL, NIL, thePrStats);
				{ Handle the special case of out of disk space, by failing with right message. }
			IF PrError =  -1 {iPrSavPFil} THEN Failure(errSpooling, 0);

				{ We are past the potential failures.  Clear the error signal. }
			Success (fi);
			PrClose;
			DisposHandle(Handle(thePrintRec));
		END;		{ cPrint }
	END;		{ CASE aCmdNumber }
			
		{ If we succeeded, we return here, and can set the flag to continue printing more documents. }
	proceed := TRUE;
END;	{ TFracAppPrintHandler.Print }


	{ We need to have a DoPrinting method to handle the menu command.
		This does nothing more than pass the buck to the Print method, but we need the
		override to have the proper flow of control. }
FUNCTION  TFracAppPrintHandler.DoPrintingCommand(aCmdNumber: CmdNumber): TCommand;  OVERRIDE;
VAR
	proceed:	Boolean;
BEGIN
	DoPrintingCommand := Print (aCmdNumber, proceed);
END;	{ DoPrintingCommand }


{$POP}		{ Restore the compiler state. }
